<template>
  <div class="sensor-card" :class="comfortLevel">
    <div class="sensor-header">
      <h3>{{ title }}</h3>
      <span class="status-indicator" :class="comfortLevel"></span>
    </div>
    <div class="sensor-data">
      <div class="data-value">
        <span class="value">{{ value }}</span>
        <span class="unit">{{ unit }}</span>
      </div>
      <div class="data-icon">
        <Icon :icon="icon" width="48" height="48" />
      </div>
    </div>
    <div class="sensor-footer">
      <span class="timestamp">更新于: {{ formattedTimestamp }}</span>
    </div>
  </div>
</template>

<script setup>
import { Icon } from '@iconify/vue'
import { computed } from 'vue'

const props = defineProps({
  title: String,
  value: [Number, String],
  unit: String,
  icon: String,
  comfortLevel: String,
  timestamp: String
})

const formattedTimestamp = computed(() => {
  const date = new Date(props.timestamp)
  return date.toLocaleTimeString() + ' ' + date.toLocaleDateString()
})
</script>

<style scoped>
.sensor-card {
  background: white;
  border-radius: 16px;
  padding: 20px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  border-left: 4px solid #4CAF50;
  overflow: hidden;
}

.sensor-card.uncomfortable {
  border-left-color: #F44336;
}

.sensor-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
}

.sensor-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.sensor-header h3 {
  margin: 0;
  color: #333;
  font-size: 1.2rem;
  font-weight: 500;
}

.status-indicator {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: #4CAF50;
}

.status-indicator.uncomfortable {
  background: #F44336;
}

.sensor-data {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.data-value {
  display: flex;
  align-items: baseline;
}

.value {
  font-size: 3rem;
  font-weight: 600;
  color: #333;
  transition: color 0.3s;
}

.unit {
  font-size: 1.5rem;
  color: #666;
  margin-left: 4px;
  transition: color 0.3s;
}

.sensor-footer {
  margin-top: 16px;
  font-size: 0.9rem;
  color: #888;
  transition: color 0.3s;
}

.timestamp {
  display: block;
  text-align: right;
}

.sensor-card.uncomfortable .value,
.sensor-card.uncomfortable .unit,
.sensor-card.uncomfortable .timestamp {
  color: #F44336;
}
</style>